package com.ruoyi.memoryleak;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/oom/3")
public class OOMController3 {

    // 模拟一个“服务”，返回大量数据
    @GetMapping("/big-data")
    public List<BigDataEntity> getBigData() {
        // 错误！尝试一次性获取并返回海量数据
        return simulateHugeDatabaseQuery();
    }

    private List<BigDataEntity> simulateHugeDatabaseQuery() {
        List<BigDataEntity> hugeList = new ArrayList<>();
        // 模拟查询返回 100,000 条记录，每条记录占用一定内存
        for (int i = 0; i < 100_000; i++) {
            hugeList.add(new BigDataEntity(
                    i,
                    "Sample User Name " + i,
                    "sample.email" + i + "@example.com",
                    "This is a large text field that simulates a lot of data stored in a single entity...",
                    "Additional payload to make the object heavier..."
            ));
        }
        return hugeList;
    }

    // 模拟一个占用内存较大的实体类

}
